package com.itheima.controller.backend.security;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 新增体检套餐
     */
    @PostMapping("/add")
    public Result add(
            @RequestParam("checkgroupIds") String checkgroupIds,
            @RequestBody Setmeal setmeal) {

        boolean success = setmealService.addSetmeal(setmeal);
        if (success) {
            return new Result(true, "添加成功");
        } else {
            return new Result(false, "添加失败");
        }
    }
    /**
     * 查询所有体检套餐
     */
    @PostMapping("/findAll")
    public List<String> findAll()
        {
        return setmealService.findAll();
        }
    /**
     * id查询体检套餐
     */
    @GetMapping("/findById")
    public Setmeal findById(@RequestParam Integer id) {
        return setmealService.findById(id);
    }
    /**
     * 分页查询所有体检套餐
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Pageable pageable = PageRequest.of(queryPageBean.getCurrentPage() , queryPageBean.getPageSize());
        if (queryString != null && !queryString.isEmpty()) {
            // 判断是否为 code 查询（可自定义逻辑）
            if (isCodeQuery(queryString)) {
                return setmealService.findPageByCode(pageable, queryString);
            } else {
                return setmealService.findPageByName(pageable, queryString);
            }
        }
        // 当 queryString 为空时，调用默认分页查询方法
        else
        {
            return findPageDefault(pageable);
        }
    }

    private boolean isCodeQuery(String keyword) {
        // 可以根据业务逻辑判断是否是 code 查询，例如只包含字母数字时认为是 code
        return keyword.matches("[a-zA-Z0-9]+");
    }

    private PageResult findPageDefault(Pageable pageable) {
        return setmealService.findPageDefault(pageable);
    }

    @PostMapping("/findPageByCode")
    public PageResult findPageByCode(@RequestBody QueryPageBean queryPageBean) {
        Pageable pageable = PageRequest.of(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        return setmealService.findPageByCode(pageable, queryPageBean.getQueryString());
    }
    @PostMapping("/findPageByName")
    public PageResult findPageByName(@RequestBody QueryPageBean queryPageBean) {
        Pageable pageable = PageRequest.of(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        return setmealService.findPageByName(pageable, queryPageBean.getQueryString());
    }
    /**
     * 修改检查项
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal) {
        System.out.println("Received Setmeal: " + setmeal);

        if (setmeal.getId() == null) {
            return new Result(false, "ID 不能为空");
        }

        boolean success = setmealService.updateSetmeal(setmeal);
        return new Result(success, success ? "修改成功" : "修改失败");
    }
    /**
     * 图片上传接口
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            // 使用 AliOssUtil 工具类上传图片到 OSS
            String url = aliOssUtil.upload(imgFile.getBytes(), imgFile.getOriginalFilename());

            // 返回上传结果
            return new Result(true, "图片上传成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "图片上传失败");
        }
    }


}
